package config

import (
	"jmicro/app/pkg/options"
	"jmicro/pkg/app"
	cliflag "jmicro/pkg/common/cli/flag"
	"jmicro/pkg/log"
)

type Config struct {
	Log       *log.Options              `json:"log" mapstructure:"log"`
	Server    *options.ServerOptions    `json:"server" mapstructure:"server"`
	Registry  *options.RegistryOptions  `json:"registry" mapstructure:"registry"`
	Jwt       *options.JwtOptions       `json:"jwt" mapstructure:"jwt"`
	Sms       *options.SmsOptions       `json:"sms" mapstructure:"sms"`
	Redis     *options.RedisOptions     `json:"redis" mapstruct:"redis"`
	Telemetry *options.TelemetryOptions `json:"telemetry" mapstruct:"telemetry"`
	Mysql     *options.MySQLOptions     `json:"mysql" mapstruct:"mysql"`
}

func (c *Config) Validate() []error {
	var errors []error
	errors = append(errors, c.Log.Validate()...)
	errors = append(errors, c.Server.Validate()...)
	errors = append(errors, c.Registry.Validate()...)
	errors = append(errors, c.Jwt.Validate()...)
	errors = append(errors, c.Sms.Validate()...)
	errors = append(errors, c.Redis.Validate()...)
	errors = append(errors, c.Telemetry.Validate()...)
	errors = append(errors, c.Mysql.Validate()...)
	return errors
}

func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
	c.Log.AddFlags(fss.FlagSet("logs"))
	c.Server.AddFlags(fss.FlagSet("server"))
	c.Registry.AddFlags(fss.FlagSet("registry"))
	c.Jwt.AddFlags(fss.FlagSet("jwt"))
	c.Sms.AddFlags(fss.FlagSet("sms"))
	c.Redis.AddFlags(fss.FlagSet("redis"))
	c.Telemetry.AddFlags(fss.FlagSet("telemetry"))
	c.Mysql.AddFlags(fss.FlagSet("mysql"))
	return fss
}
func New() *Config {
	return &Config{
		Log:       log.NewOptions(),
		Server:    options.NewServerOptions(),
		Registry:  options.NewRegistryOptions(),
		Jwt:       options.NewJwtOptions(),
		Sms:       options.NewSmsOptions(),
		Redis:     options.NewRedisOptions(),
		Telemetry: options.NewTelemetryOptions(),
		Mysql:     options.NewMySQLOptions(),
	}
}

var _ app.CliOptions = &Config{}
